<?php
namespace app\admin\controller;

use app\plug\controller\qiniu\Qiniu;

use think\Db;
use think\Model;


/**
 * 文章管理
 */
class Article extends Base
{
    /**
     * @throws
     * 文章分类
     * @return [type] [description]
     */
    public function sort()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 查询数据
        $model = Db::name('article_sort');
        $list = $model->where('is_del',0)->order("id desc")->paginate($pageSize);

        $this->assign('total',$list->total());
        $this->assign('page',$list->render());
        $this->assign('list',$list);

        return $this->fetch();
    }


    /**
     * yang yang
     * 添加分类
     */
    public function add()
    {
        if(request()->isPost()){

            $model = Db::name('article_sort');

            // 图片资源
            $file   = request()->file('picture');

            // 本地路径
            $filePath = $file->getRealPath();

            // 文件后缀
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

            // 七牛存储文件名
            $file_name = 'images/article_sort/'.date("Ymd").rand(1000,9999).'.'.$ext;

            // 实例化七牛云
            $qiniu = new Qiniu();

            // 上传至七牛云
            $re = $qiniu->upload_pic($filePath,$file_name);
            if($re['code'] == 0){
                $this->error("图片未成功上传七牛云".$re['data']);
            }

            // 存入数据库
            $data['sort']      = input("sort");
            $data['status']    = input("status");
            $data['picture']    = $re["data"];
            $data['create_time'] = time();
            $repeat=$model->where('sort', $data['sort'])->select();
            if($repeat){
                $this->error("该分类已存在，请不要重复添加！");
            }
            $ret =$model->insert($data);
            if($ret){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }

        }

        return $this->fetch();
    }


    /**
     * yang yang
     * @throws
     * 编辑分类
     */
    public function edit()
    {
        $id = input('id');

        $model = Db::name('article_sort');;

        // 更新数据
        if(request()->isPost()){

            $data['sort'] = input('sort');
            $data['status'] = input('status');

            // 图片是否被编辑
            $file   = request()->file('picture');
            if($file){
                // 本地路径
                $filePath = $file->getRealPath();

                // 文件后缀
                $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

                // 七牛存储文件名
                $file_name = 'images/article_sort/'.date("Ymd").rand(1000,9999).'.'.$ext;

                // 实例化七牛云
                $qiniu = new Qiniu();

                // 上传至七牛云
                $re = $qiniu->upload_pic($filePath,$file_name);
                if($re['code'] == 0){
                    $this->error("图片未成功上传七牛云".$re['data']);
                }

                $data['picture']    = $re["data"];

            }


            $re = $model->where('id',$id)->update($data);
            if($re===false){
                $this->error("编辑失败");
            }
            $this->success("编辑成功");
        }

        // 详情
        $info = $model->where('id',$id)->find();
        $this->assign('info',$info);

        return $this->fetch();
    }

    /**
     * yang yang
     * 编辑文章分类审核状态
     */
    public function status(){
        if (request()->isAjax()) {
            $id = input('id');
            $res = Db::name('article_sort')->where('id', $id)->update(['status' => input('status')]);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }

    /**
     * yang yang
     * 删除分类
     */
    public function del()
    {
        $id = input('id');
        if (request()->isAjax()) {

            $res = Db::name('article_sort')->where('id', $id)->update(['is_del' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }

    /**
     * @return mixed
     * yang yang
     * 文章列表
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 查询数据
        $model = Db::name('article');
        $list = $model->where('is_del',0)->order("id desc")->paginate($pageSize);

        $this->assign('total',$list->total());
        $this->assign('page',$list->render());
        $this->assign('list',$list);

        return $this->fetch();
    }

    /**
     * yang yang
     * @return mixed
     * 添加文章
     */
    public function add_article()
    {
        if(request()->isPost()){

            $model = Db::name('article');
            // 存入数据库
            $data['sort']      = input("sort");
            $status = Db::name('article_sort')->where('sort',$data['sort'] )->field('status,id')->find();
            if($status['status']==1){
                $data['status'] = 0;
            }
            if($status['status']==0){
                $data['status'] = 1;
            }
            $data['sort_id'] =  $status['id'];
            $data['sort_status'] = $status['status'];
            $data['title']    = input("title");
            $data['edit']    = 0;
            $data['author']    = input("author");
            $data['content']    = input("content");
            $data['create_time'] = time();
            $ret =$model->insert($data);
            if($ret){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }

        }
        $sort = Db::name('article_sort')->where('is_del',0)->select();
        $this->assign('sort',$sort);
        $this->assign('author',$this->user_name);
        return $this->fetch();
    }

    /**
     * yang yang
     * @return mixed
     * 编辑文章
     */
    public function edit_article()
    {
        $id = input('id');

        $model = Db::name('article');;

        // 更新数据
        if(request()->isPost()){

            $data['sort'] = input("sort");
            $data['title'] = input("title");
            $data['edit']    = 1;
            $data['author'] = input("author");
            $data['content'] = input("content");
            $status = Db::name('article_sort')->where('sort',$data['sort'] )->field('status,id')->find();
            if($status['status']==1){
                $data['status'] = 0;
            }
            if($status['status']==0){
                $data['status'] = 1;
            }
            $data['sort_id'] =  $status['id'];
            $data['sort_status'] = $status['status'];


            $re = $model->where('id',$id)->update($data);
            if($re===false){
                $this->error("编辑失败");
            }
            $this->success("编辑成功");
        }

        // 详情
        $info = $model->where('id',$id)->find();
        $this->assign('info',$info);
        $sort = Db::name('article_sort')->where('is_del',0)->select();
        $this->assign('sort',$sort);

        return $this->fetch();
    }

    /**
     * yang yang
     * 是否同意文章发布
     */
    public function article_status(){
        if (request()->isAjax()) {
            $id = input('id');
            $res = Db::name('article')->where('id', $id)->update(['status' => input('status')]);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }

    /**
     * yang yang
     * 删除文章
     */
    public function article_del()
    {
        $id = input('id');
        if (request()->isAjax()) {

            $res = Db::name('article')->where('id', $id)->update(['is_del' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }
    
}
